<?php
/**
 * @file
 * Process theme data.
 *
 */
$theme_path = drupal_get_path( 'theme', 'dshive' );
#require_once $theme_path . '/includes/menu.inc';
#require_once $theme_path . '/includes/template-icons.inc';


// load module-specific files in includes/modules directory.
/*
$includes = file_scan_directory( $theme_path . '/includes/modules', '/\.inc$/' );
foreach( $includes as $include )
{
	if( module_exists( $include->name ))
	{
		require_once $include->uri;
	}
}
*/

function dshive_preprocess_page( &$variables )
{
	$variables['navbar_toggle'] = '<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">'
		. '<span class="icon-bar"></span>'
		. '<span class="icon-bar"></span>'
		. '<span class="icon-bar"></span>'
		. '</button>';
	
}


/*
 * Implements hook_menu_link
 * Apply bootstrap menu classes to all menu blocks in the 
 * navigation region and the main-menu block by default.
 */
function dshive_menu_link( array $variables )
{
  $element = $variables['element'];
  $sub_menu = '';
  
  if ($element['#below']) {

    // Prevent dropdown functions from being added to management menu as to not affect navbar module.
    if (($element['#original_link']['menu_name'] == 'management') && (module_exists('navbar'))) {
      $sub_menu = drupal_render($element['#below']);
    }

    else {
      // Add our own wrapper
      unset($element['#below']['#theme_wrappers']);
      $sub_menu = '<ul class="dropdown-menu">' . drupal_render($element['#below']) . '</ul>';
      $element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';
      $element['#localized_options']['attributes']['data-toggle'] = 'dropdown';

      // Check if this element is nested within another
      if ((!empty($element['#original_link']['depth'])) && ($element['#original_link']['depth'] > 1)) {
        // Generate as dropdown submenu
        $element['#attributes']['class'][] = 'dropdown-submenu';
      }
      else {
        // Generate as standard dropdown
        $element['#attributes']['class'][] = 'dropdown';
        $element['#localized_options']['html'] = TRUE;
        $element['#title'] .= ' <span class="caret"></span>';
      }

      // Set dropdown trigger element to # to prevent inadvertant page loading with submenu click
      $element['#localized_options']['attributes']['data-target'] = '#';
    }
  }
 // Issue #1896674 - On primary navigation menu, class 'active' is not set on active menu item.
 // @see http://drupal.org/node/1896674
 if (($element['#href'] == $_GET['q'] || ($element['#href'] == '<front>' && drupal_is_front_page())) && (empty($element['#localized_options']['language']) || $element['#localized_options']['language']->language == $language_url->language)) {
   $element['#attributes']['class'][] = 'active';
 }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

/**
 * Implements hook_css_alter().
 * api-ref: https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_css_alter/7
 * $css: an array with all css-files, sorted by groups (CSS_SYSTEM, CSS_DEFAULT, CSS_THEME)
 */
function dshive_css_alter( &$css )
{
	#$theme_path = drupal_get_path( 'theme', 'dshive' );
	
	// automatically remove all other css files (other than group CSS_THEME).
	/*
	foreach( $css as $key => $value ) {
		if( $value['group'] != CSS_THEME )
		{
			$exclude[$key] = FALSE;
		}
	}
	$css = array_diff_key( $css, $exclude );
	*/
	
	// manually remove some core CSS.
	/*
	$exclude = array(
		'modules/aggregator/aggregator.css' => FALSE,
		'modules/block/block.css' => FALSE,
	);
	$css = array_diff_key( $css, $exclude );
	*/

	// add twitter bootstrap CDN files.
	#$bootstrap_cdn = '//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css';
	
	/*
	$css[$bootstrap_cdn] = array(
		'data' => $bootstrap_cdn,
		'type' => 'external',
		'every_page' => TRUE,
		'media' => 'all',
		'preprocess' => TRUE,
		'group' => CSS_THEME,
		'browsers' => array( 'IE' => TRUE, '!IE' => TRUE ),
		'weight' => 0,
	);
	*/
	
	#print "<pre>"; print_r( $css ); print "</pre>";
}


/**
 * Implements hook_js_alter().
 */
function dshive_js_alter(&$javascript)
{
	// replace drupal-jquery with newer (needed for bootstrap).
	// load from cdn (googleapis.com), local load fallback.
	global $base_url;
	
	$jquery_version = '1.10.1';
	$jquery_cdn = '//ajax.googleapis.com/ajax/libs/jquery/' . $jquery_version . '/jquery.min.js';
	$jquery_local = $base_url . '/' . drupal_get_path( 'theme', 'dshive' ) . '/assets/js/vendor/jquery-1.10.1.min.js?v=' . $jquery_version;
	
	$javascript['misc/jquery.js']['data'] = $jquery_cdn;
	$javascript['misc/jquery.js']['version'] = $jquery_version;
	
	$group = $javascript['misc/jquery.js']['group'] = JS_LIBRARY;
	$weight = $javascript['misc/jquery.js']['weight'] = -20;
	
	drupal_add_js( 'window.jQuery || document.write(\'<script type="text/javascript" src="' . $jquery_local . '"><\/script>\')',
		array( 'type' => 'inline', 'scope' => 'header', 'group' => $group, 'every_page' => TRUE, 'weight' => $weight));

	#print "<pre>"; print_r( $javascript ); print "</pre>";
}
